//
//  NSValueTransformer+LL.m
//  lrlzapp
//
//  Created by dl on 16/2/26.
//  Copyright © 2016年 lrlz. All rights reserved.
//

#import "NSValueTransformer+LL.h"
#import <Mantle/Mantle.h>

NSString *const MTLCommonValueTransformerName = @"MTLCommonValueTransformerName";
NSString *const MTLDateTimeValueTransformerName = @"MTLDateTimeValueTransformerName";

@implementation NSValueTransformer (LL)

+ (void)load {
    MTLValueTransformer *commonvalueTransformer =
            [MTLValueTransformer transformerUsingForwardBlock:^id(id value, BOOL *success, NSError *__autoreleasing *error) {
                TransformerForwardNull

                return [NSString stringWithFormat:@"%@", value];
            }                                    reverseBlock:^id(id value, BOOL *success, NSError *__autoreleasing *error) {
                TransformerReverseNull

                return [NSString stringWithFormat:@"%@", value];
            }];

    [NSValueTransformer setValueTransformer:commonvalueTransformer forName:MTLCommonValueTransformerName];

    MTLValueTransformer *dateTimeTransformer =
            [MTLValueTransformer transformerUsingForwardBlock:^id(id value, BOOL *success, NSError *__autoreleasing *error) {
                TransformerForwardNull

                NSDateFormatter *fm = [[NSDateFormatter alloc] init];
                [fm setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
                return [fm dateFromString:value];
            }                                    reverseBlock:^id(id value, BOOL *success, NSError *__autoreleasing *error) {
                TransformerReverseNull

                NSDateFormatter *fm = [[NSDateFormatter alloc] init];
                [fm setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
                return [fm stringFromDate:value];
            }];
    [NSValueTransformer setValueTransformer:dateTimeTransformer forName:MTLDateTimeValueTransformerName];
}

@end
